package com.vgemv.jsutilitysdkdemo.module.Menu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemStyleDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;
import com.vgemv.jsutilitysdkdemo.module.Menu.SlowLive.SlowLiveItemView;
import com.vgemv.jsutilitysdkdemo.module.Menu.SlowLive.SlowLiveVm;
import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.log.JsLogManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuFragment.JsMenuFragment.JsMenuFragment;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVmDefault;

import java.util.ArrayList;

public class MenuActivity extends AppCompatActivity {

    private RelativeLayout mRelativeLayout;
    private FrameLayout mv_slowLiveContainer;
    private FrameLayout mv_mainMenuContainer;
    private MenuActivityVm mvm = new MenuActivityVm();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRelativeLayout = new RelativeLayout(this);
        mRelativeLayout.setId(View.generateViewId());
        setContentView(mRelativeLayout);
        setupUi();
        updateUi(mvm);
    }

    void setupUi() {
        //SlowLiveContainer;
        {
            mv_slowLiveContainer = new FrameLayout(this);
            mv_slowLiveContainer.setId(View.generateViewId());
            mRelativeLayout.addView(mv_slowLiveContainer);
            {
                //SlowLive
                JsMenuFragment fragment = new JsMenuFragment();
                fragment.moveToParentView(this, mv_slowLiveContainer);

                final int top = (mvm.mi_slowLiveViewHeight - SlowLiveVm.itemViewHeight)/2;
                final int itemViewRight_itemViewLeft = 30;
                final int parentLeft_itemViewRight = 60;

                fragment.mBasicCallback = new JsMenuViewBasicCallback() {
                    @Override
                    public Rect jsMenuViewSetItemOffset(JsMenuView fragment, int index) {
                        Rect rect = new Rect();
                        if(index == 0) {
                            rect.top = top;
                            rect.left = parentLeft_itemViewRight;
                            rect.right = itemViewRight_itemViewLeft;
                        }
                        else if(index == fragment.vms().size() - 1) {
                            rect.top = top;
                            rect.left = itemViewRight_itemViewLeft;
                            rect.right = itemViewRight_itemViewLeft;
                        }
                        else {
                            rect.top = top;
                            rect.left = itemViewRight_itemViewLeft;
                            rect.right = itemViewRight_itemViewLeft;
                        }
                        return rect;
                    }

                    @Override
                    public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                        return null;
                    }

                    @Override
                    public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

                    }

                    @Override
                    public JsMenuViewVm jsMenuViewSetVm(JsMenuView fragment) {
                        JsMenuViewVm vm = new JsMenuViewVm();
                        vm.mi_layoutStyle = 1;
                        vm.mColor_background = Color.argb(255,200,200,200);
                        return vm;
                    }
                    @Override
                    public JsMenuItemView jsMenuViewCreateItemView(JsMenuView fragment, Context context) {
                        SlowLiveItemView itemView = new SlowLiveItemView(context);
                        return itemView;
                    }

                    @Override
                    public void jsMenuViewAfterCreateView(JsMenuView fragment) {

                    }

                    @Override
                    public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                        return SlowLiveVm.testArray();
                    }

                    @Override
                    public void jsMenuViewClickItem(JsMenuView fragment, int i, JsMenuItemVm model) {
                        JsLogManager.logc_d("MenuActivity/jsMenuViewClickItem","index is %d",i);
                    }
                };
            }
        }

        {
            //MainMenuContainer;
            mv_mainMenuContainer = new FrameLayout(this);
            mv_mainMenuContainer.setId(View.generateViewId());
            mRelativeLayout.addView(mv_mainMenuContainer);
            {
                //MainMenu
                JsMenuFragment fragment = new JsMenuFragment();
                fragment.moveToParentView(this, mv_mainMenuContainer);

                final int top = (mvm.mi_mainMenuHeight - mvm.mi_mainMenuItemHeight)/2;
                int buttonCount = 3;

                final int cellCellSpacing = (JsGeometryManager.getScreenSizeInPixel(this).getWidth() - mvm.mi_mainMenuItemWidth * buttonCount) / ((buttonCount -1) + 2 * mvm.mi_cellParantPaddingScale);
                final int cellParentSpacing = cellCellSpacing * mvm.mi_cellParantPaddingScale;
                fragment.mBasicCallback = new JsMenuViewBasicCallback() {
                    @Override
                    public Rect jsMenuViewSetItemOffset(JsMenuView fragment, int index) {
                        Rect rect = new Rect();
                        if(index == 0) {
                            //第一个
                            rect.top = top;
                            rect.left = cellParentSpacing;
                            rect.right = cellCellSpacing/2;
                        }
                        else if(index == fragment.vms().size() - 1) {
                            //最后一个
                            rect.top = top;
                            rect.left = cellCellSpacing/2;
                            rect.right = cellParentSpacing;
                        }
                        else {
                            //中间的
                            rect.top = top;
                            rect.left = cellCellSpacing/2;
                            rect.right = cellCellSpacing/2;
                        }
                        return rect;
                    }

                    @Override
                    public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                        return null;
                    }

                    @Override
                    public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

                    }

                    @Override
                    public JsMenuViewVm jsMenuViewSetVm(JsMenuView fragment) {
                        JsMenuViewVm vm = new JsMenuViewVm();
                        vm.mi_layoutStyle = 1;
                        vm.mColor_background = Color.argb(255,200,100,100);
                        vm.mI_initialSelectedIndex = 0;
                        vm.mb_enableRefreshAndLoadmore = true;
                        return vm;
                    }

                    @Override
                    public JsMenuItemView jsMenuViewCreateItemView(JsMenuView fragment, Context context) {
                        return null;
                    }

                    @Override
                    public void jsMenuViewAfterCreateView(JsMenuView fragment) {

                    }

                    @Override
                    public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                        ArrayList<JsMenuItemVm> al = new ArrayList<>();
                        {
                            JsMenuItemVm vm = new JsMenuItemVmDefault(null,"图文直播",null,mvm.mi_mainMenuItemWidth,mvm.mi_mainMenuItemHeight);

                            JsMenuItemStyleDefault unselectedStyle = (JsMenuItemStyleDefault)vm.getUnselectedStyle();
                            vm.getUnselectedStyle().mi_cornerRadius = 0;
                            vm.getUnselectedStyle().mi_borderWidth = 0;
                            vm.getUnselectedStyle().mI_backgroundColor = Color.WHITE;

                            JsMenuItemStyleDefault selectedStyle = unselectedStyle.copy();
                            selectedStyle.mI_backgroundColor = Color.YELLOW;
                            vm.setSelectedStyle(selectedStyle);

                            al.add(vm);
                        }
                        {
                            JsMenuItemVm vm = new JsMenuItemVmDefault(null,"聊天",null,mvm.mi_mainMenuItemWidth,mvm.mi_mainMenuItemHeight);

                            JsMenuItemStyleDefault unselectedStyle = (JsMenuItemStyleDefault)vm.getUnselectedStyle();
                            vm.getUnselectedStyle().mi_cornerRadius = 0;
                            vm.getUnselectedStyle().mi_borderWidth = 0;
                            vm.getUnselectedStyle().mI_backgroundColor = Color.WHITE;

                            JsMenuItemStyleDefault selectedStyle = unselectedStyle.copy();
                            selectedStyle.mI_backgroundColor = Color.YELLOW;
                            vm.setSelectedStyle(selectedStyle);

                            al.add(vm);
                        }
                        {
                            JsMenuItemVm vm = new JsMenuItemVmDefault(null,"相关报道",null,mvm.mi_mainMenuItemWidth,mvm.mi_mainMenuItemHeight);

                            JsMenuItemStyleDefault unselectedStyle = (JsMenuItemStyleDefault)vm.getUnselectedStyle();
                            vm.getUnselectedStyle().mi_cornerRadius = 0;
                            vm.getUnselectedStyle().mi_borderWidth = 0;
                            vm.getUnselectedStyle().mI_backgroundColor = Color.WHITE;

                            JsMenuItemStyleDefault selectedStyle = unselectedStyle.copy();
                            selectedStyle.mI_backgroundColor = Color.YELLOW;
                            vm.setSelectedStyle(selectedStyle);

                            al.add(vm);
                        }
                        return al;
                    }

                    @Override
                    public void jsMenuViewClickItem(JsMenuView fragment, int i, JsMenuItemVm model) {
                        JsLogManager.logc_d("MenuActivity/jsMenuViewClickItem","index is %d",i);
                    }
                };
            }
        }
    }

    public void updateUi(MenuActivityVm vm) {
        {
            //mv_slowLiveContainer
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,mvm.mi_slowLiveViewHeight);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            mv_slowLiveContainer.setLayoutParams(layoutParams);
            mv_slowLiveContainer.setBackgroundColor(Color.argb(255,0,0,0));
        }

        {
            //MainMenu
            //mv_slowLiveContainer
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,mvm.mi_mainMenuHeight);
            layoutParams.addRule(RelativeLayout.BELOW,mv_slowLiveContainer.getId());
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            mv_mainMenuContainer.setLayoutParams(layoutParams);
            mv_mainMenuContainer.setBackgroundColor(Color.argb(255,255,200,200));
        }
    }
}